// pages/edit/edit.js
const {detail, option, update, add, list, follow, faceMatch, session, rubbish, levelOption, baiduIdentify} = require("../../api/index.js")
const des = require('../../utils/des.js')
const utils = require("../../utils/index.js")
Page({

/**
* 页面的初始数据
*/data: {
imgIcon: "../../static/upload.png",
editStatus: false,
baseURL:'',
sessionReadArr:[],
detailList: null,
id: "",
cross:"",
ruleForm:{},
userid:getApp().globalData.userInfo.id,
userInfo:getApp().globalData.userInfo,
ro:{
},
xiangmumingcheng:"",
xiangmufenlei:"",
xiangmujieduan:"",
xiangmudangqianjieduan:"",
suoshulingyu:"",
suoshuxingye:"",
xiangmujianjie:"",
shenqingdongji:"",
tijiaoshijian:"请选择时间",
showtijiaoshijian:false,
sfsh: '待审核',
shhf:"",
fuzerenzhanghao:"",
fuzerenxingming:"",
canjiayixiang:"",
xuehao:"",
xueshengxingming:"",
zhuanye:"",
shoujihaoma:"",
chengshi:"",
yuanqu:"",
jiedao:"",
shenqingshuliang: '1',
shenqingshijian:"请选择时间",
showshenqingshijian:false,
daoshigonghaoIndex: 0,
daoshigonghaoList: [],
xuexiaomingcheng:"",
xueyuanmingcheng:"",
xiangmujianjie:"",
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
let userid
if (options?.id) {
this.setData({
editStatus: true
})
}
let nowTable = wx.getStorageSync("nowTable");
const res = await session(nowTable)
if(res.data.code==0){
getApp().globalData.userInfo=res?.data
userid = res?.data.id
this.setData({
userid
})
}
let baseURL =wx.getStorageSync('baseURL') + '/'
const id = getApp().globalData.detailId
this.setData({
refid:id,
baseURL
})
//人脸识别
 this.setData({
   sfsh: '待审核',
 })
 this.setData({
   shenqingshuliang: '1',
 })
const daoshigonghaores = await option('daoshi/daoshigonghao')
daoshigonghaores.data.unshift('请选择导师工号')
this.setData({
daoshigonghaoList: daoshigonghaores.data
})
let  ro=this.data.ro;
if(options?.cross){
var obj = wx.getStorageSync('crossObj');
let refobjempty={}
for (var o in obj){
if(o=='xiangmumingcheng'){
   refobjempty["xiangmumingcheng"]=obj[o]
ro.xiangmumingcheng = true;
continue;
}else{
}
if(o=='xiangmufenlei'){
   refobjempty["xiangmufenlei"]=obj[o]
ro.xiangmufenlei = true;
continue;
}else{
}
if(o=='xiangmujieduan'){
   refobjempty["xiangmujieduan"]=obj[o]
ro.xiangmujieduan = true;
continue;
}else{
}
if(o=='xiangmudangqianjieduan'){
   refobjempty["xiangmudangqianjieduan"]=obj[o]
ro.xiangmudangqianjieduan = true;
continue;
}else{
}
if(o=='suoshulingyu'){
   refobjempty["suoshulingyu"]=obj[o]
ro.suoshulingyu = true;
continue;
}else{
}
if(o=='suoshuxingye'){
   refobjempty["suoshuxingye"]=obj[o]
ro.suoshuxingye = true;
continue;
}else{
}
if(o=='xiangmujianjie'){
   refobjempty["xiangmujianjie"]=obj[o]
ro.xiangmujianjie = true;
continue;
}else{
}
if(o=='shenqingdongji'){
   refobjempty["shenqingdongji"]=obj[o]
ro.shenqingdongji = true;
continue;
}else{
}
if(o=='tijiaoshijian'){
   refobjempty["tijiaoshijian"]=obj[o]
ro.tijiaoshijian = true;
continue;
}else{
}
if(o=='fuzerenzhanghao'){
   refobjempty["fuzerenzhanghao"]=obj[o]
ro.fuzerenzhanghao = true;
continue;
}else{
}
if(o=='fuzerenxingming'){
   refobjempty["fuzerenxingming"]=obj[o]
ro.fuzerenxingming = true;
continue;
}else{
}
if(o=='canjiayixiang'){
   refobjempty["canjiayixiang"]=obj[o]
ro.canjiayixiang = true;
continue;
}else{
}
if(o=='xuehao'){
   refobjempty["xuehao"]=obj[o]
ro.xuehao = true;
continue;
}else{
}
if(o=='xueshengxingming'){
   refobjempty["xueshengxingming"]=obj[o]
ro.xueshengxingming = true;
continue;
}else{
}
if(o=='zhuanye'){
   refobjempty["zhuanye"]=obj[o]
ro.zhuanye = true;
continue;
}else{
}
if(o=='shoujihaoma'){
   refobjempty["shoujihaoma"]=obj[o]
ro.shoujihaoma = true;
continue;
}else{
}
if(o=='chengshi'){
   refobjempty["chengshi"]=obj[o]
ro.chengshi = true;
continue;
}else{
}
if(o=='yuanqu'){
   refobjempty["yuanqu"]=obj[o]
ro.yuanqu = true;
continue;
}else{
}
if(o=='jiedao'){
   refobjempty["jiedao"]=obj[o]
ro.jiedao = true;
continue;
}else{
}
if(o=='shenqingshuliang'){
   refobjempty["shenqingshuliang"]=obj[o]
ro.shenqingshuliang = true;
continue;
}else{
}
if(o=='shenqingshijian'){
   refobjempty["shenqingshijian"]=obj[o]
ro.shenqingshijian = true;
continue;
}else{
}
if(o=='daoshigonghao'){
   refobjempty["daoshigonghao"]=obj[o]
ro.daoshigonghao = true;
continue;
}else{
}
if(o=='xuexiaomingcheng'){
   refobjempty["xuexiaomingcheng"]=obj[o]
ro.xuexiaomingcheng = true;
continue;
}else{
}
if(o=='xueyuanmingcheng'){
   refobjempty["xueyuanmingcheng"]=obj[o]
ro.xueyuanmingcheng = true;
continue;
}else{
}
}
let  statusColumnName=wx.getStorageSync('statusColumnName');
statusColumnName=statusColumnName.replace('[',"").replace(']',"");
this.setData({
ro,
cross:options?.cross,
statusColumnName
})
this.setData(refobjempty)
}
if(id){
// 如果上一级页面传递了id，获取改id数据信息
const data=getApp().globalData.detailList;
let dataEmpty={};
daoshigonghaores.data.map((item, index) => {
if (item == data.daoshigonghao) {
 this.setData({
daoshigonghaoIndex: index,
daoshigonghao: item
 })
}else if (this.data.daoshigonghaoList.length == 1) {
this.setData({
daoshigonghaoIndex: 0,
daoshigonghao: v
 })
}
})
for (var o in data){
if(o=='xiangmumingcheng') {
 if(!this.data.xiangmumingcheng){
  dataEmpty["xiangmumingcheng"] = data[o]
 }
}
if(o=='xiangmufenlei') {
 if(!this.data.xiangmufenlei){
  dataEmpty["xiangmufenlei"] = data[o]
 }
}
if(o=='xiangmujieduan') {
 if(!this.data.xiangmujieduan){
  dataEmpty["xiangmujieduan"] = data[o]
 }
}
if(o=='xiangmudangqianjieduan') {
 if(!this.data.xiangmudangqianjieduan){
  dataEmpty["xiangmudangqianjieduan"] = data[o]
 }
}
if(o=='suoshulingyu') {
 if(!this.data.suoshulingyu){
  dataEmpty["suoshulingyu"] = data[o]
 }
}
if(o=='suoshuxingye') {
 if(!this.data.suoshuxingye){
  dataEmpty["suoshuxingye"] = data[o]
 }
}
if(o=='xiangmujianjie') {
 if(!this.data.xiangmujianjie){
  dataEmpty["xiangmujianjie"] = data[o]
 }
}
if(o=='shenqingdongji') {
 if(!this.data.shenqingdongji){
  dataEmpty["shenqingdongji"] = data[o]
 }
}
if(o=='tijiaoshijian') {
 if(!this.data.tijiaoshijian){
  dataEmpty["tijiaoshijian"] = data[o]
 }
}
if(o=='fuzerenzhanghao') {
 if(!this.data.fuzerenzhanghao){
  dataEmpty["fuzerenzhanghao"] = data[o]
 }
}
if(o=='fuzerenxingming') {
 if(!this.data.fuzerenxingming){
  dataEmpty["fuzerenxingming"] = data[o]
 }
}
if(o=='canjiayixiang') {
 if(!this.data.canjiayixiang){
  dataEmpty["canjiayixiang"] = data[o]
 }
}
if(o=='xuehao') {
 if(!this.data.xuehao){
  dataEmpty["xuehao"] = data[o]
 }
}
if(o=='xueshengxingming') {
 if(!this.data.xueshengxingming){
  dataEmpty["xueshengxingming"] = data[o]
 }
}
if(o=='zhuanye') {
 if(!this.data.zhuanye){
  dataEmpty["zhuanye"] = data[o]
 }
}
if(o=='shoujihaoma') {
 if(!this.data.shoujihaoma){
  dataEmpty["shoujihaoma"] = data[o]
 }
}
if(o=='chengshi') {
 if(!this.data.chengshi){
  dataEmpty["chengshi"] = data[o]
 }
}
if(o=='yuanqu') {
 if(!this.data.yuanqu){
  dataEmpty["yuanqu"] = data[o]
 }
}
if(o=='jiedao') {
 if(!this.data.jiedao){
  dataEmpty["jiedao"] = data[o]
 }
}
if(o=='shenqingshuliang') {
 if(!this.data.shenqingshuliang){
  dataEmpty["shenqingshuliang"] = data[o]
 }
}
if(o=='shenqingshijian') {
 if(!this.data.shenqingshijian){
  dataEmpty["shenqingshijian"] = data[o]
 }
}
if(o=='daoshigonghao') {
 if(!this.data.daoshigonghao){
  dataEmpty["daoshigonghao"] = data[o]
 }
}
if(o=='xuexiaomingcheng') {
 if(!this.data.xuexiaomingcheng){
  dataEmpty["xuexiaomingcheng"] = data[o]
 }
}
if(o=='xueyuanmingcheng') {
 if(!this.data.xueyuanmingcheng){
  dataEmpty["xueyuanmingcheng"] = data[o]
 }
}
}
const url = wx.getStorageSync("baseURL") + "/"
const detailList = data
let  objtemp= {
detailList,
 tijiaoshijian:utils.getCurrentDate("yMDhms"),
 sfsh: '待审核',
 shenqingshuliang: '1',
 shenqingshijian:utils.getCurrentDate("yMDhms"),
}
this.setData(objtemp);

this.setData(dataEmpty)
}else {
this.setData({
})
}
// ss读取
let sessionReadArr=[]
 if(!this.data.xuehao&&getApp().globalData.userInfo.xuehao){
 let xuehao= getApp().globalData.userInfo.xuehao
 ro.xuehao=true
 this.setData({
   xuehao,
  })
 sessionReadArr.push('xuehao')
 }
 if(!this.data.xueshengxingming&&getApp().globalData.userInfo.xueshengxingming){
 let xueshengxingming= getApp().globalData.userInfo.xueshengxingming
 ro.xueshengxingming=true
 this.setData({
   xueshengxingming,
  })
 sessionReadArr.push('xueshengxingming')
 }
 if(!this.data.zhuanye&&getApp().globalData.userInfo.zhuanye){
 let zhuanye= getApp().globalData.userInfo.zhuanye
 ro.zhuanye=true
 this.setData({
   zhuanye,
  })
 sessionReadArr.push('zhuanye')
 }
 if(!this.data.shoujihaoma&&getApp().globalData.userInfo.shoujihaoma){
 let shoujihaoma= getApp().globalData.userInfo.shoujihaoma
 ro.shoujihaoma=true
 this.setData({
   shoujihaoma,
  })
 sessionReadArr.push('shoujihaoma')
 }
 if(!this.data.chengshi&&getApp().globalData.userInfo.chengshi){
 let chengshi= getApp().globalData.userInfo.chengshi
 ro.chengshi=true
 this.setData({
   chengshi,
  })
 sessionReadArr.push('chengshi')
 }
 if(!this.data.yuanqu&&getApp().globalData.userInfo.yuanqu){
 let yuanqu= getApp().globalData.userInfo.yuanqu
 ro.yuanqu=true
 this.setData({
   yuanqu,
  })
 sessionReadArr.push('yuanqu')
 }
 if(!this.data.jiedao&&getApp().globalData.userInfo.jiedao){
 let jiedao= getApp().globalData.userInfo.jiedao
 ro.jiedao=true
 this.setData({
   jiedao,
  })
 sessionReadArr.push('jiedao')
 }
ro.shenqingshuliang=true
this.setData({
cross:options?.cross,
ro,
id,
})

if (wx.getStorageSync('raffleType') ) {
 wx.removeStorageSync('raffleType')
 setTimeout(() => {
  this.submit()
 }, 300)
}









this.setData({
tijiaoshijian:utils.getCurrentDate("yMDhms")
})















this.setData({
shenqingshijian:utils.getCurrentDate("yMDhms")
})




},
getUUID () {
return new Date().getTime();
},
onUnload: function () {
getApp().globalData.editorContent = ''
},
onShow() {
},
 commentScore(e){
 if(this.data?.commentScore){
  this.setData({
   commentScore:e.detail
  })
 }
 },































xiangmujianjieInput(e){
this.setData({
xiangmujianjie: e.detail.value // 每次输入变化时更新文本框的值
});},




xiangmujianjieInput(e) {
this.setData({
xiangmujianjie: e.detail.value // 每次输入变化时更新文本框的值
});
},











 ontijiaoshijianTap(){
this.setData({
showtijiaoshijian: true,
})
let c = this.data;
},
tijiaoshijianTap(e) {
this.setData({
tijiaoshijian: e.detail.data
})

let c = this.data;
},




shhfInput(e){
this.setData({
shhf: e.detail.value // 每次输入变化时更新文本框的值
});},




shhfInput(e) {
this.setData({
shhf: e.detail.value // 每次输入变化时更新文本框的值
});
},



















































 onshenqingshijianTap(){
this.setData({
showshenqingshijian: true,
})
let c = this.data;
},
shenqingshijianTap(e) {
this.setData({
shenqingshijian: e.detail.data
})

let c = this.data;
},





async getfollow(pathName, refColumn, refColumnValue, suiColumnName) {
const {
data
} = await follow(`xiangmushenqing/${refColumn}`, refColumnValue)
let tempObj = {};
tempObj[suiColumnName] = data[suiColumnName];
this.setData(tempObj);
},

async daoshigonghaoChange(e) {
const selectedIndex = e.detail.value;
let  daoshigonghao=this.data.daoshigonghaoList[selectedIndex]
this.setData({
daoshigonghaoIndex: selectedIndex,
daoshigonghao
});
const {data} = await follow('daoshi/daoshigonghao', daoshigonghao)
let h = this.data;
let c=this.data
let d2 = this.data
},










async submit() {
let that = this
var query = wx.createSelectorQuery();

// 在 Page 中的某个方法中调用
query.select('#xiangmujianjie').boundingClientRect();
query.exec(function (res) {
//res就是 所有标签为v1的元素的信息 的数组
that.setData({
xiangmujianjie: res[0]?.dataset?.info
})
})
if(this.data.tijiaoshijian?.includes("请选择") || this.data.tijiaoshijian==""){
wx.showToast({
icon: "none",
title: `提交时间不能为空`,
})
return
}
if(this.data.shenqingshijian?.includes("请选择") || this.data.shenqingshijian==""){
wx.showToast({
icon: "none",
title: `申请时间不能为空`,
})
return
}

const baseURL = wx.getStorageSync('baseURL') + "/"
const regex = new RegExp(baseURL, "g");
const obj={
xiangmumingcheng: this.data.xiangmumingcheng,
xiangmufenlei: this.data.xiangmufenlei,
xiangmujieduan: this.data.xiangmujieduan,
xiangmudangqianjieduan: this.data.xiangmudangqianjieduan,
suoshulingyu: this.data.suoshulingyu,
suoshuxingye: this.data.suoshuxingye,
xiangmujianjie: this.data.xiangmujianjie,
shenqingdongji: this.data.shenqingdongji,
tijiaoshijian: this.data.tijiaoshijian,
shhf:this.data.shhf,
fuzerenzhanghao: this.data.fuzerenzhanghao,
fuzerenxingming: this.data.fuzerenxingming,
canjiayixiang: this.data.canjiayixiang,
xuehao: this.data.xuehao,
xueshengxingming: this.data.xueshengxingming,
zhuanye: this.data.zhuanye,
shoujihaoma: this.data.shoujihaoma,
chengshi: this.data.chengshi,
yuanqu: this.data.yuanqu,
jiedao: this.data.jiedao,
shenqingshuliang: this.data.shenqingshuliang,
shenqingshijian: this.data.shenqingshijian,
daoshigonghao:this.data.daoshigonghaoList[this.data.daoshigonghaoIndex]?this.data.daoshigonghaoList[this.data.daoshigonghaoIndex]:'',
xuexiaomingcheng: this.data.xuexiaomingcheng,
xueyuanmingcheng: this.data.xueyuanmingcheng,
sfsh: this.data.sfsh, // 新增，确保提交时带上审核状态
}
const detailId= getApp().globalData.detailId
const tableName= `xiangmushenqing`
//跨表计算判断
var obj2;
var  ruleForm=obj
obj2 = ruleForm
this.data.refid==""? ruleForm['refid']= getApp().globalData.detailId:""
ruleForm['userid']=getApp().globalData.userInfo.id;
var userInfo=getApp().globalData.userInfo
const sessionReadArr=this.data.sessionReadArr
const phonePattern = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
const mobilePattern = /^(?:\+?86)?1[3-9]\d{9}$/;
const emailPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/;
const urlPattern = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)+[/#?]?.*$/;
 if(!this.data.xiangmumingcheng ){
  wx.showToast({
   icon: "none",
   title: `项目名称不能为空`,
  })
  return
 }

























 if(!this.data.xiangmujianjie ){
  wx.showToast({
   icon: "none",
   title: `项目简介不能为空`,
  })
  return
 }














































const valueshoujihaoma = this.data.shoujihaoma;

if (!mobilePattern.test(valueshoujihaoma)) {
wx.showToast({
icon: "none",
title: `手机号码应输入手机格式`,
})
return
}















const valueshenqingshuliang = this.data.shenqingshuliang;
const isIntegershenqingshuliang = /^[-+]?\d+$/.test(valueshenqingshuliang);
if(!isIntegershenqingshuliang && this.data.shenqingshuliang){
wx.showToast({
icon: "none",
title: `申请数量应输入整数`,
})
return
}





















//更新跨表属性
var crossuserid;
var crossrefid;
var crossoptnum;
if(this.data.cross) {
wx.setStorageSync('crossCleanType', true);
var statusColumnName = wx.getStorageSync('statusColumnName');
var statusColumnValue = wx.getStorageSync('statusColumnValue');
if (statusColumnName != '') {
obj2 = wx.getStorageSync('crossObj');
if (!statusColumnName.startsWith("[")) {
for (var o in obj2) {
if (statusColumnName==o){
obj2[o] = statusColumnValue;
}

}
var table = wx.getStorageSync('crossTable');

await update(table, obj2)
} else {

crossuserid =getApp().globalData.userInfo.id
crossrefid =  this.data.id
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
}
}
}
 ruleForm?.shhf?ruleForm.shhf=this.data.shhf:''
if(crossrefid && crossuserid) {
ruleForm['crossuserid'] =getApp().globalData.userInfo.id;
ruleForm['crossrefid'] =obj2.id;
this.setData({
ruleForm
})
let params = {
page: 1,
limit: 10,
crossuserid: crossuserid,
crossrefid: crossrefid,
}
const tips = wx.getStorageSync('tips')
let corssRes = await list(`xiangmushenqing`, params)
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.match(/\d+/g);
if (corssRes?.data?.total >= parseInt(crossoptnum)) {
wx.showToast({
icon: "none",
title: tips,
})
wx.removeStorageSync('crossCleanType');
return ;
}
else {
//跨表计算
if (ruleForm.id ) {
await update(`xiangmushenqing`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
 await add(`xiangmushenqing`, ruleForm)
}
}
}
else {
//跨表计算
if (ruleForm.id || this.data.editStatus) {
this.data.editStatus?ruleForm['id']= getApp().globalData.detailId:""
await update(`xiangmushenqing`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
await add(`xiangmushenqing`, ruleForm)
}
}
getApp().globalData.editorContent=''
wx.showToast({
title: '提交成功',
icon: "none"
})
const preId = getApp().globalData.detailId

if(table){
let res = await detail(table, preId)
if(res.code==0){
getApp().globalData.detailList = res.data
}

}
wx.navigateBack({
delta: 1,
complete: () => {
// 触发事件通知，传递需要更新的数据
const pages = getCurrentPages();
if (pages.length >= 1) {
const prePage = pages[pages.length - 1];
prePage.onLoad(); //
}
}
})

},
onHide() {

},


/**
* 生命周期函数--监听页面卸载
*/
onUnload() {

},


/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage() {

}
})